/**
 * 
 */
package com.thor.palmmall.rest.utils;

import java.util.List;

import com.thor.commons.exception.ThorServiceException;
import com.thor.util.ApplicationContextUtils;
import com.thor.util.JPushParamProvider;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class JPushUtils {
  public static void push(String alert, String uuid, List<String> targets)
      throws ThorServiceException {
    if (targets != null)
      push(alert, uuid, targets.toArray(new String[targets.size()]));
    else
      push(alert, uuid);
  }

  public static void push(String alert, String uuid, String... targets)
      throws ThorServiceException {
    System.out.print("wllllcssss" + targets);
    JPushClient jpushClient = new JPushClient(getParamProvider().getMasterSecret(),
        getParamProvider().getAppKey(), getParamProvider().getMaxRetryTimes());
    PushPayload payload = buildPushObject_alias_alert(alert, uuid, targets);

    try {
      jpushClient.sendPush(payload);
    } catch (APIConnectionException e) {
      throw new ThorServiceException("Jpush:Connection error, should retry later");

    } catch (APIRequestException e) {
      throw new ThorServiceException(
          "Should review the error, and fix the request:" + e.getErrorMessage());
    }

  }

  public static PushPayload buildPushObject_alias_alert(String alert, String uuid,
      String... alias) {
    Audience ai = Audience.alias(alias);
    PushPayload pp = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(ai)
        .setNotification(Notification.newBuilder()
            .addPlatformNotification(
                AndroidNotification.newBuilder().setAlert(alert).addExtra("id", uuid).build())
            .addPlatformNotification(IosNotification.newBuilder().setAlert(alert)
                .addExtra("id", uuid).autoBadge().setSound("happy.caf").build())
            .build())
        .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
    return pp;
  }

  private static JPushParamProvider getParamProvider() {
    return ApplicationContextUtils.getBean(JPushParamProvider.class);
  }

  public static void main(String[] args) {
    System.out.println("dddddddddddddddddddddddddddddddd");
    JPushClient jpushClient = new JPushClient("f7e7a1bc447008c7f9fbfbd3",
        "a035d59d5a3af341917eed17", 3);

    // For push, all you need do is to build PushPayload object.
    PushPayload payload = buildPushObject_all_all_alert("看到了请通知wolv", new String[] {
        "su" });

    try {
      PushResult result = jpushClient.sendPush(payload);
      // LOG.info("Got result - " + result);
      System.out.println(result);
    } catch (APIConnectionException e) {
      // Connection error, should retry later
      // LOG.error("Connection error, should retry later", e);
      System.out.println("eeeee");

    } catch (APIRequestException e) {
      // Should review the error, and fix the request
      // LOG.error("Should review the error, and fix the request", e);
      // LOG.info("HTTP Status: " + e.getStatus());
      // LOG.info("Error Code: " + e.getErrorCode());
      // LOG.info("Error Message: " + e.getErrorMessage());
      System.out.println("ffff");

    }
  }

  private static PushPayload buildPushObject_all_all_alert(String alert, String... alias) {
    Audience ai = Audience.alias(alias);
    PushPayload pp = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(ai)
        .setNotification(Notification.newBuilder()
            .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).build())
            .addPlatformNotification(IosNotification.newBuilder().setAlert(alert).autoBadge()
                .setSound("happy.caf").build())
            .build())
        .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();

    return pp;
  }
}
